<template>
  <el-dialog title="添加分类" :visible="showDialog" @close="closeFn">
    <el-form ref="form" :model="formData" label-width="120px" :rules="rules">
      <el-form-item label="标题:" prop="title">
        <el-input v-model="formData.title" placeholder="请添加标题" />
      </el-form-item>
      <el-form-item label="描述:" prop="desc">
        <el-input v-model="formData.desc" placeholder="请添加描述" />
      </el-form-item>
      <el-form-item label="封面" prop="image">
        <el-input v-model="formData.image" placeholder="请添加封面" />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="submit">添加</el-button>
        <el-button @click="closeFn">取消</el-button>

      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addcategory } from '@/api/category'
export default {
  inject: ['reload'],
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        desc: '',
        image: ''
      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ],
        image: [
          { required: true, message: '封面不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 新增
    async submit() {
      // 表单校验
      await this.$refs.form.validate()
      // 新增数据
      await addcategory(this.formData)
      //   关闭弹窗
      this.closeFn()
      this.reload()
    },
    // 关闭
    closeFn() {
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

  <style>

  </style>

